package com.erpanderp.wam.usm.model.service.timesheet;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;
import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class TimesheetDAO {
	public Timesheet createTimesheet(Timesheet timesheet) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.save(timesheet);
			session.getTransaction().commit();
			return timesheet;
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null && session.isOpen()) session.close();
			return null;
		}
	}
	
	public boolean createTimesheetsForWamAllEmployees(List<Timesheet> timesheetsList) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			for(Timesheet timesheet : timesheetsList){
			 session.save(timesheet);
			}
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null && session.isOpen()) session.close();
			return false;
		}
	}
	
	
	
	public boolean saveTimesheets(List<Timesheet> timesheetsList) {
		System.out.println("TIMESHEETS SIZE I DAO "+timesheetsList.size());
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			for(Timesheet timeSheet : timesheetsList){
			session.saveOrUpdate(timeSheet);
			}
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null && session.isOpen()) session.close();
			return false;
		}
		
	}
	
	
	public Timesheet updateTimesheet(Timesheet timesheet) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(timesheet);
			session.getTransaction().commit();
			return timesheet;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public Timesheet getTimeSheetByYearMonthEmployeeId(Integer year, String month, Long employeeId){
		
		Timesheet timesheet = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Timesheet t where t.status = :status and t.year = :year and t.month = :month" +
									" and t.timesheetStatus != :timesheetStatus and t.employee.status = :employeeStatus";
			
			if(employeeId != null && !employeeId.equals("")){
				queryString += " and t.employee.employeeId = :employeeId";
			}
			
		
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("status", CommonLookup.STATUS_ACTIVE).setInteger("year", year).setString("month", month).setLong("employeeId", employeeId).setString("timesheetStatus", "NOTHING").setString("employeeStatus", CommonLookup.STATUS_ACTIVE);

			timesheet =(Timesheet) query.uniqueResult();
			session.getTransaction().commit();
			return timesheet;
			
			
				
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}	
	}
	
	
	public List<Timesheet> getTimesheets(Integer year, String month, String employeeCode, String employeeName, Long loginEmployeeId){
		List<Timesheet> timesheets = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Timesheet t where t.status = :status and t.year = :year and t.month = :month" +
									" and t.timesheetStatus != :timesheetStatus" +
									" and t.employee.employeeId in (select ear.employee.employeeId from EmployeeAndReporting ear where ear.projectManager.employeeId = :loginEmployeeId or ear.competencelead.employeeId = :loginEmployeeId)" +
									" and t.employee.status = :employeeStatus";
			
			if(employeeCode != null && !employeeCode.equals("")){
				queryString += " and t.employee.employeeCode = :employeeCode";
			}
			
			if(employeeName != null && !employeeName.equals("")){
				queryString += " and (t.employee.firstName = :employeeName or t.employee.lastName = :employeeName)";
			}
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("status", CommonLookup.STATUS_ACTIVE).setInteger("year", year).setString("month", month).setLong("loginEmployeeId", loginEmployeeId).setString("timesheetStatus", CommonLookup.TIMESHEET_STATUS_NOT_SUBMITTED).setString("employeeStatus", CommonLookup.STATUS_ACTIVE);
			
			if(employeeCode != null && !employeeCode.equals("")){
				query.setString("employeeCode", employeeCode);
			}
			
			if(employeeName != null && !employeeName.equals("")){
				query.setString("employeeName", employeeName);
			}
			
			
			timesheets =(List<Timesheet>) query.list();
			
			System.out.println("Timesheets: " + timesheets);
			session.getTransaction().commit();
			return timesheets;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}	
	}
	
	/** 
	 * Get all Timesheets of irrespective of login user
	 * @param year
	 * @param month
	 * @param employeeCode
	 * @param employeeName
	 * @return timesheets of active users
	 */
	public List<Timesheet> getTimesheets(Integer year, String month, String employeeCode, String employeeName){
		List<Timesheet> timesheets = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Timesheet t where t.status = :status and t.year = :year and t.month = :month" +
									" and t.timesheetStatus != :timesheetStatus and t.employee.status = :employeeStatus";
			
			if(employeeCode != null && !employeeCode.equals("")){
				queryString += " and t.employee.employeeCode = :employeeCode";
			}
			
			if(employeeName != null && !employeeName.equals("")){
				queryString += " and (t.employee.firstName = :employeeName or t.employee.lastName = :employeeName)";
			}
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("status", CommonLookup.STATUS_ACTIVE).setInteger("year", year).setString("month", month).setString("timesheetStatus", "NOTHING").setString("employeeStatus", CommonLookup.STATUS_ACTIVE);
			
			if(employeeCode != null && !employeeCode.equals("")){
				query.setString("employeeCode", employeeCode);
			}
			
			if(employeeName != null && !employeeName.equals("")){
				query.setString("employeeName", employeeName);
			}
			
			
			timesheets =(List<Timesheet>) query.list();
			
			System.out.println("Timesheets: " + timesheets);
			session.getTransaction().commit();
			return timesheets;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}	
	}
	
	public List<Timesheet> getTimesheets(Long approverId, String status){
		List<Timesheet> timesheets = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Timesheet t where t.status = :status and t.approver.employeeId = :approverId" +
									" and t.timesheetStatus = :timesheetStatus";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("status", CommonLookup.STATUS_ACTIVE).setLong("approverId", approverId).setString("timesheetStatus", status);
			
			timesheets =(List<Timesheet>) query.list();
			
			System.out.println("Timesheets: " + timesheets);
			session.getTransaction().commit();
			return timesheets;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}	
	}
	
	public Timesheet getTimesheet(String month, Integer year, Long employeeId){
		Timesheet timesheet = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Timesheet t where t.month = :month and t.year = :year and t.employee.employeeId = :employeeId";
			Query query = session.createQuery(queryString);
			query.setString("month", month).setInteger("year",year).setLong("employeeId", employeeId);
			List<Timesheet> temp = (List<Timesheet>) query.list();
			session.getTransaction().commit();
			
			if (temp != null && temp.size() > 0){
				timesheet = temp.get(0);
				return timesheet;
			}
			return null;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Timesheet getTimesheetById(Long timesheetId){
		Timesheet timesheet = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			timesheet = (Timesheet) session.load(Timesheet.class, timesheetId);
			
			session.getTransaction().commit();
			
			return timesheet;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String a[]){
		//System.out.println(new TimesheetDAO().getTimesheets(26l, CommonLookup.TIMESHEET_STATUS_TO_BE_APPROVED));
	}
	
	
	
	
	
	
}
